package com.hzcx.app.simplechat.ui.friend;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.friend.adapter.FriendLableListAdapter;
import com.hzcx.app.simplechat.ui.friend.bean.FriendLableListBean;
import com.hzcx.app.simplechat.ui.friend.contract.FriendLableListContract;
import com.hzcx.app.simplechat.ui.friend.event.FriendLableRefreshEvent;
import com.hzcx.app.simplechat.ui.friend.presenter.FriendLableListPresenter;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 标签列表页
 */
public class FriendLableListActivity extends BaseActivity<FriendLableListPresenter> implements FriendLableListContract.View {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.btn_create)
    Button btnCreate;
    @BindView(R.id.ll_null)
    LinearLayout llNull;
    @BindView(R.id.tv_add)
    TextView tvAdd;
    @BindView(R.id.rv_lable)
    RecyclerView rvLable;
    @BindView(R.id.ll_content)
    LinearLayout llContent;

    private List<FriendLableListBean> lableData;
    private FriendLableListAdapter lableListAdapter;

    @Override
    public int getLayout() {
        return R.layout.activity_friend_lable_list;
    }

    @Override
    public void initPresenter() {
        mPresenter = new FriendLableListPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("标签");

        lableData = new ArrayList<>();
        lableListAdapter = new FriendLableListAdapter(lableData);
        rvLable.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvLable.setAdapter(lableListAdapter);
    }

    @Override
    public void initData() {
        mPresenter.getFriendLableList(this);

        lableListAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()) {
                case R.id.tv_delete:
                    mPresenter.deleteFriendLable(FriendLableListActivity.this, lableData.get(position).getLable_id(), position);
                    break;

                case R.id.cons_content:
                    startActivity(new Intent(FriendLableListActivity.this, FriendLableInfoActivity.class)
                            .putExtra(FriendLableInfoActivity.INTENT_LABLE_ID, lableData.get(position).getLable_id()+"")
                            .putExtra(FriendLableInfoActivity.INTENT_IS_EDIT, true));
                    break;
            }
        });
    }

    @OnClick({R.id.btn_create, R.id.tv_add})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.btn_create: // 添加标签
            case R.id.tv_add:
                startActivity(new Intent(this, FriendLableInfoActivity.class));
                break;
        }
    }

    /**
     * 标签列表获取成功
     *
     * @param data
     */
    @Override
    public void lableResult(List<FriendLableListBean> data) {
        try {
            lableData.clear();
            lableData.addAll(data);
            lableListAdapter.notifyDataSetChanged();
            if (lableData.size() <= 0) {
                llNull.setVisibility(View.VISIBLE);
                llContent.setVisibility(View.GONE);
            } else {
                llNull.setVisibility(View.GONE);
                llContent.setVisibility(View.VISIBLE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 标签删除成功
     *
     * @param pos
     */
    @Override
    public void deleteSuccess(int pos) {
        try {
            lableData.remove(pos);
            lableListAdapter.notifyDataSetChanged();
            if (lableData.size() <= 0) {
                llNull.setVisibility(View.VISIBLE);
                llContent.setVisibility(View.GONE);
            } else {
                llNull.setVisibility(View.GONE);
                llContent.setVisibility(View.VISIBLE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 刷新标签列表
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshLable(FriendLableRefreshEvent event) {
        mPresenter.getFriendLableList(this);
    }
}
